package jjn.round1;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Jjn
 * @since 2022/10/2 13:53
 */
public class LeetCode414_ThirdMaximumNumber {
    public int thirdMax(int[] nums) {
        Arrays.sort(nums);
        int length = nums.length;
        int start = length - 1;
        Set<Integer> set = new HashSet<>();
        while (start >= 0) {
            if (set.add(nums[start])) {
                if (set.size() == 3) {
                    return nums[start];
                }
            }
            start--;
        }
        return nums[length - 1];
    }
}
